<?php

namespace Addons\IntegralShop\Controller;

use Addons\IntegralShop\Repositor\IsUserRepositor;
use Mp\Controller\AddonsController;


use Addons\IntegralShop\Repositor\IsGoodRepositor;
use Addons\IntegralShop\Repositor\IsOrderRepositor;

/**
 * 积分商城后台管理控制器
 * @author 16,sg
 */
class WebController extends AddonsController
{
    /**
     * 商品管理
     */
    public function goods()
    {
        $this->setModel(get_addon_model('goods'));
        $this->common_lists();
    }

    public function good_add()
    {
        $this->setModel(get_addon_model('good_add'));
        $this->setAddSuccessUrl(create_addon_url('goods'));
        $this->common_add();
    }

    public function good_edit()
    {
        $this->setModel(get_addon_model('good_add'));
        $this->setEditMap(array('id' => I('get.id')));
        $this->setEditSuccessUrl(create_addon_url('goods'));
        $this->common_edit();
    }

    public function good_delete($id)
    {
        $repo = new IsGoodRepositor();
        if ($repo->delete($id)) {
            $this->success('删除成功');
        } else {
            $this->error('失败，请重试');
        }
    }

    /**
     * 订单列表
     */
    public function orders()
    {
        $this->setModel(get_addon_model('orders'));
        $this->common_lists();
    }

    /**
     * 订单处理 - 成功
     */
    public function order_set_success()
    {
        $this->setModel(get_addon_model('order_set_success'));
        $this->setEditMap(array('id' => I('get.id')));
        $this->setEditSuccessUrl(create_addon_url('orders'));
        $this->common_edit();
    }

    /**
     * 订单处理 - 拒绝
     * @param $id
     */
    public function order_set_reject()
    {
        if (IS_POST) {
            $id = I('get.id', 0, 'intval');
            $errMsg = I('err_msg', '');

            if (!$id) $this->error("非法标识");
            if ($errMsg == '') $this->error("拒绝原因不能为空");

            // 修改状态
            $orderRepo = new IsOrderRepositor();
            if (!$orderRepo->rejectOrder($id, $errMsg)) {
                $this->error($orderRepo->getError());
            } else {
                $this->success("操作成功", create_addon_url('orders'));
            }
        }

        $this->setModel(get_addon_model('order_set_reject'));
        $this->setEditMap(array('id' => I('get.id')));
        $this->setEditSuccessUrl(create_addon_url('orders'));
        $this->common_edit();
    }

    /**
     * 积分记录
     */
    public function spreads()
    {
        $this->setModel(get_addon_model('spreads'));
        $this->common_lists();
    }

    /**
     * 用户积分信息
     */
    public function user_mark()
    {
        $this->setModel(get_addon_model('user_mark'));
        $this->common_lists();
    }
}
